package com.example.lotterysystem.controller;
import com.example.LotterysystemApplication;
import com.example.lotterysystem.common.pojo.CommonResult;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.CreatePrizeParam;
import com.example.lotterysystem.controller.param.PageListParam;
import com.example.lotterysystem.controller.result.FindPrizeListResult;
import com.example.lotterysystem.controller.result.PrizeInfo;
import com.example.lotterysystem.service.dto.FindPageListDTO;
import com.example.lotterysystem.service.dto.PrizeDTO;
import com.example.lotterysystem.service.PictureService;
import com.example.lotterysystem.service.PrizeService;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.List;

@RestController
public class PrizeContrller {

    private Logger logger= LoggerFactory.getLogger(UserController.class);


    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeService prizeService;
    @Autowired
    private LotterysystemApplication lotterysystemApplication;


    @RequestMapping("/pic/upload")
    public String upLoad(MultipartFile pic){
        return pictureService.savePicture(pic);
    }


    /**
     * 创建奖品
     *  RequestPart:用于接收表单数据的 multipart/form-data
     *
     * @param param
     * @param picFile
     * @return
     */
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(@Valid @RequestPart("param") CreatePrizeParam param,
                                          @RequestPart("prizePic") MultipartFile picFile) {
        logger.info("createPrize CreatePrizeParam:{}",
                JacksonUtil.writeValueAsString(param));
        return CommonResult.success(
                prizeService.createPrize(param, picFile));
    }




    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList( PageListParam param){
        if(param==null) return CommonResult.error(998,"参数为空");

        System.out.println(param);

        FindPageListDTO<PrizeDTO> prizeDTOFindPageListDTO = prizeService.selectPrizeList(param);

        return CommonResult.success(convertToFindPrizeListResult(prizeDTOFindPageListDTO));
    }


    private FindPrizeListResult convertToFindPrizeListResult(FindPageListDTO<PrizeDTO> prizeDTOFindPageListDTO){
        FindPrizeListResult findPrizeListResult=new FindPrizeListResult();

        findPrizeListResult.setTotal(prizeDTOFindPageListDTO.getTotal());
        List<PrizeDTO> records = prizeDTOFindPageListDTO.getRecords();
        List<PrizeInfo> list=new ArrayList<>();
        for(PrizeDTO record:records){
            PrizeInfo info=new PrizeInfo();
            info.setPrizeId(record.getPrizeId());
            info.setPrizeName(record.getPrizeName());
            info.setDescription(record.getDescription());
            info.setPrice(record.getPrice());
            info.setImageUrl(record.getImageUrl());
            list.add(info);
        }

        findPrizeListResult.setRecords(list);

        return findPrizeListResult;

    }
}
